<template>
	<view class="container">
		<u-navbar title="创建简历1/2" :custom-back="handleToBack"></u-navbar>
		<view v-if="data.dataList && data.dataList.basic && data.dataList.contact && data.utype == 2">
			<u-cell-group>
				<u-cell-item required isLink title="姓名" :title-style="data.titleStyle" :use-label-slot="true"
					:arrow="false">
					<template v-slot:label>
						<u-field :border-bottom="false" label-width="0"
							:field-style="data.dataList.basic.fullname ? data.labelStyle:data.labelStyle2"
							v-model="data.dataList.basic.fullname" placeholder="请输入姓名"
							:placeholder-style="data.placeholderStyle" />
					</template>
				</u-cell-item>
				<u-cell-item required isLink @click="data.isSex= true" :title-style="data.titleStyle"
					:label-style="data.dataList.basic.sex != 0 ? data.labelStyle:data.labelStyle2" title="性别"
					:label="data.dataList.basic.sex != 0 ? data.dataList.basic.sex_text : '请选择性别'">
				</u-cell-item>
				<u-cell-item required isLink @click="data.isBirthday = true" title="出生日期" :title-style="data.titleStyle"
					:label="data.dataList.basic.birthday ? $u.timeFormat(new Date(data.dataList.basic.birthday), 'yyyy-mm') : '请选择出生年月'"
					:label-style="data.dataList.basic.birthday != 0 ? data.labelStyle:data.labelStyle2">
				</u-cell-item>
				<u-cell-item required isLink title="联系电话" :title-style="data.titleStyle" :use-label-slot="true"
					:arrow="false">
					<template v-slot:label>
						<u-field :border-bottom="false" label-width="0"
							:field-style="data.dataList.contact.mobile ? data.labelStyle:data.labelStyle2"
							v-model="data.dataList.contact.mobile" placeholder="请输入联系电话"
							:placeholder-style="data.placeholderStyle" />
					</template>
				</u-cell-item>
				<u-cell-item required isLink title="" :title-style="data.titleStyle" :use-label-slot="true"
					:arrow="false">
					<template v-slot:label>
						<view style="display: flex;">
							<u-field :border-bottom="false" label-width="0" @input="changeInput"
								:field-style="data.dataList.contact.weixin ? data.labelStyle:data.labelStyle2"
								v-model="data.dataList.contact.weixin" placeholder="请输入微信号"
								:placeholder-style="data.placeholderStyle" />
						</view>
					</template>
					<template v-slot:title>
						<view class="titleleft">
							<text>微信号</text>
							<u-checkbox @change="checkboxChange" shape="circle" v-model="data.phoneNum"><text
									style="white-space: nowrap !important;">同手机号</text></u-checkbox>
						</view>
					</template>
				</u-cell-item>
				<u-cell-item required isLink title="" :title-style="data.titleStyle" :use-label-slot="true"
					:arrow="false">
					<template v-slot:label>
						<view style="display: flex;justify-content:space-between;">
							<input disabled @click="data.isWork = true" type="text" :style="data.labelStyle"
								:value="data.dataList.basic.enter_job_time?$u.timeFormat(new Date(data.dataList.basic.enter_job_time), 'yyyy-mm'):data.dateshow"
								placeholder="请选择" :placeholder-style="data.placeholderStyle">
							<u-icon name="arrow-right" @click="data.isWork = true"></u-icon>
						</view>
					</template>
					<template v-slot:title>
						<view class="titleleft" @click.stop>
							<text>开始工作时间</text>
							<u-checkbox shape="circle" @change="enterjobtime" v-model="data.thisyear"><text
									style="white-space: nowrap !important;">应届生/无经验</text></u-checkbox>
						</view>
					</template>
				</u-cell-item>
				<u-cell-item required isLink @click="data.show = true" title="最高学历" :title-style="data.titleStyle"
					:label="data.dataList.basic.education_text || '请选择最高学历'"
					:label-style="data.dataList.basic.education_text ? data.labelStyle:data.labelStyle2">
				</u-cell-item>
				<view class="" style="background-color: #f6f6f6;height: 10rpx;width: 100%;">
				</view>
				<u-cell-item required isLink @click="data.isCategory=true" title="期望职位" :title-style="data.titleStyle"
					:label="data.formData.category_text || '请选择期望职位'"
					:label-style="data.formData.category_text ? data.labelStyle:data.labelStyle2">
				</u-cell-item>
				<u-cell-item required isLink @click="data.isDistrict=true" title="期望地区" :title-style="data.titleStyle"
					:label="data.formData.district_text || '请选择期望地区'"
					:label-style="data.formData.district_text? data.labelStyle:data.labelStyle2">
				</u-cell-item>
				<u-cell-item required isLink @click="data.isWage=true" title="期望薪资" :title-style="data.titleStyle"
					:label="data.formData.wage_text || '请选择期望薪资'"
					:label-style="data.formData.wage_text? data.labelStyle:data.labelStyle2">
				</u-cell-item>
				<u-cell-item :border-top="false" :border-bottom="false" required isLink @click="data.isJobStatus = true"
					title="求职状态" :title-style="data.titleStyle" :label="data.jobStatus || '请选择求职状态'"
					:label-style="data.jobStatus? data.labelStyle:data.labelStyle2">
				</u-cell-item>
			</u-cell-group>
		</view>
		<!-- //性别 -->
		<u-picker v-model="data.isSex" :range="data.sexList" mode="selector"
			@confirm="sexConfirm" :default-selector="data.sexIndex"></u-picker>
		<!-- //出生日期 -->
		<u-picker v-model="data.isBirthday" :params="data.params" @confirm="birthdayConfirm"
			:default-time="data.dataList.basic.birthday?$u.timeFormat(new Date(data.dataList.basic.birthday), 'yyyy-mm-dd'):$u.timeFormat(new Date(), 'yyyy-mm-dd')"
			mode="time"></u-picker>
		<!-- //开始工作时间 -->
		<u-picker v-model="data.isWork" :params="data.params" @confirm="workConfirm"
			:default-time="data.dataList.basic.enter_job_time?$u.timeFormat(new Date(data.dataList.basic.enter_job_time), 'yyyy-mm-dd'):$u.timeFormat(new Date(), 'yyyy-mm-dd')"
			mode="time"></u-picker>
		<!-- 学历 -->
		<u-picker v-model="data.show" @confirm="confirm" range-key="name" mode="selector" :range="data.columns"
			:default-selector="data.defaultIndex"></u-picker>
		<!-- 期望职位 -->
		<u-select v-model="data.isCategory" mode="mutil-column-auto" :list="data.categoryList"
			@confirm="categoryConfirm"></u-select>
		<!-- 期望地区 -->
		<u-select v-model="data.isDistrict" :list="data.districtList" @confirm="districtConfirm"></u-select>
		<!-- 期望薪资 -->
		<u-picker-salary v-model="data.isWage" :minwage="data.formData.minwage" :maxwage="data.formData.maxwage"
			@cancel="data.isWage = false" @close="data.isWage = false" @confirm="wageConfirm"></u-picker-salary>
		<!-- 求职状态 -->
		<u-select v-model="data.isJobStatus" :list="data.jobStatusList" value-name="id" label-name="name"
			@cancel="data.isJobStatus = false" @confirm="jobStatusconfirm"></u-select>
		<view class="btnBox">
			<button @tap="$u.throttle(toSave,2000)">下一步</button>
		</view>
	</view>
</template>

<script setup>
	import {
		ref,
		toRaw
	} from "vue"
	import {
		onReady,
		onShow,
		onLoad
	} from '@dcloudio/uni-app'
	const data = ref({
		// 求职状态
		isJobStatus: false,
		jobStatusList: [],
		jobStatus: '',
		jobStatusId: '',
		params: {
			year: true,
			month: true,
			day: false,
			hour: false,
			minute: false,
			second: false
		},
		phoneNum: false,
		utype: uni.getStorageSync('utype'),
		dataList: {
			basic: {
				fullname: "",
				sex: "",
				birthday: "",
				education: null,
				education_text: "",
				enter_job_time: ""
			},
			contact: {
				mobile: "",
				weixin: ""
			}
		},
		columns: [],
		defaultIndex: [0],
		sexList: [
			'男', '女'
		],
		sexIndex: [0],
		isBirthday: false,
		show: false,
		titleStyle: {
			'font-size': '30rpx',
			'color': '#303133'
		},
		labelStyle: {
			'font-size': '30rpx',
			'color': '#000'
		},
		labelStyle2: {
			'font-size': '28rpx',
			'color': '#c0c4cc'
		},
		placeholderStyle: "font-size: 28rpx;color:#c0c4cc",
		isWork: false,
		thisyear: false,
		formData: {
			category1: "",
			category2: "",
			category3: "",
			category_text: "",
			district1: "",
			district_text: "",
			minwage: 500,
			maxwage: 0,
			wage_text: ""
		},
		// 期望职位
		isCategory: false,
		categoryList: [],
		// 期望地区
		isDistrict: false,
		districtList: [],
		// 期望薪资
		isWage: false,
		wageList: [],
		dateshow: ""
	})
	onReady(() => {})
	// 返回上一页
	const handleToBack = () => {
		var pages = getCurrentPages();
		var page = pages[pages.length - 2];
		if (page == undefined) {
			uni.$u.route({
				type: 'reLaunch',
				url: '/pages/login/status'
			})
		} else {
			uni.$u.route({
				type: 'back'
			})
		}
	}

	// 用户在线简历数据
	const getUserdetail = async () => {
		let res = await uni.$u.api.getOnlineMange({})
		if (res) {
			data.value.dataList = res
			//期望职位
			data.value.formData.category1 = res.intention_list[0].category1
			data.value.formData.category2 = res.intention_list[0].category2
			data.value.formData.category3 = res.intention_list[0].category3
			data.value.formData.category_text = res.intention_list[0].category_text
			//期望地区
			data.value.formData.district1 = res.intention_list[0].district1
			data.value.formData.district_text = res.intention_list[0].district_text
			//期望薪资
			data.value.formData.minwage = res.intention_list[0].minwage
			data.value.formData.maxwage = res.intention_list[0].maxwage
			data.value.formData.wage_text =
				`${res.intention_list[0].minwage / 1000}K~${res.intention_list[0].maxwage/1000}K`
			// 开始工作时间
			if (!res.basic.enter_job_time) {
				console.log(res.basic.enter_job_time)
				data.value.thisyear = true
				data.value.dataList.basic.enter_job_time = ""
				data.value.dateshow = uni.$u.timeFormat(new Date(), 'yyyy-mm')
			} else {
				data.value.dataList.basic.enter_job_time = res.basic.enter_job_time * 1000

			}
			data.value.jobStatus = res.basic.current_text
			data.value.jobStatusId = res.basic.current

			if (res.contact.weixin == res.contact.mobile && res.contact.mobile) {
				data.value.phoneNum = true
			}

		}
	}
	const init = async () => {
		if (data.utype == 1) {} else {
			await getEducationList()
			await getClassifyInfo()
			await getClassifyData()
			await getUserdetail()
		}
	}
	// 修改性别
	const sexConfirm = (e) => {
		let val = e[0] == '0' ? 1 : 2
		data.value.dataList.basic.sex = val
		data.value.dataList.basic.sex_text = val == 1 ? "男" : "女"
	}
	// 修改生日
	const birthdayConfirm = (e) => {
		data.value.dataList.basic.birthday = e.year + '-' + e.month
	}
	// 修改工作时间
	const workConfirm = (e) => {
		let newdate = uni.$u.timeFormat(new Date(), 'yyyy-mm')
		let date = e.year + '-' + e.month
		if (newdate == date) {
			data.value.thisyear = true
			data.value.dataList.basic.enter_job_time = ""
			data.value.dateshow = uni.$u.timeFormat(new Date(), 'yyyy-mm')
		} else {
			data.value.thisyear = false
			data.value.dataList.basic.enter_job_time = e.year + '-' + e.month
		}

	}
	// 是否微信同号
	const checkboxChange = (e) => {
		console.log(e)
		if (e.value) {
			data.value.dataList.contact.weixin = data.value.dataList.contact.mobile
		}

	}
	// 是否应届生
	const enterjobtime = (e) => {
		console.log(e)
		if (e.value) {
			data.value.dataList.basic.enter_job_time = ""
			data.value.dateshow = uni.$u.timeFormat(new Date(), 'yyyy-mm')
		} else {
			data.value.dateshow = ""
		}
	}
	// 微信输入事件
	const changeInput = (e) => {
		if (e === data.value.dataList.contact.mobile && data.value.dataList.contact.mobile) {
			data.value.phoneNum = true
		} else {
			data.value.phoneNum = false
		}
	}

	// 修改最高学历
	const confirm = (e) => {
		let index = e[0]
		data.value.dataList.basic.education_text = data.value.columns[index].name;
		data.value.dataList.basic.education = data.value.columns[index].id;
	}
	// 获取求职状态列表
	const getClassifyInfo = async () => {
		let params = {
			type: 'current'
		}
		let res = await uni.$u.api.getClassifyInfo({
			params
		})
		data.value.jobStatusList = res
	}
	// 求职状态
	const jobStatusconfirm = async (e) => {
		data.value.jobStatus = e[0].label
		data.value.jobStatusId = e[0].value
	}
	// 下一步
	const toSave = async () => {
		let {
			fullname,
			sex,
			birthday,
			education,
			education_text,
			enter_job_time
		} = data.value.dataList.basic
		let {
			mobile,
			weixin
		} = data.value.dataList.contact


		let {
			category_text,
			district_text,
			wage_text
		} = data.value.formData
		if (!fullname) {
			return uni.showToast({
				title: '请输入姓名',
				icon: "none"

			})
		}
		if (!sex) {
			return uni.showToast({
				title: '请选择性别',
				icon: "none"
			})
		}
		if (!birthday) {
			return uni.showToast({
				title: '请选择出生年月',
				icon: "none"
			})
		}
		if (!enter_job_time && !data.value.thisyear) {
			return uni.showToast({
				title: '请选择工作时间',
				icon: "none"
			})
		}

		if (!education_text) {
			return uni.showToast({
				title: '请选择学历',
				icon: "none"
			})
		}
		if (!mobile) {
			return uni.showToast({
				title: '请输入联系电话',
				icon: "none"
			})
		}

		if (!uni.$u.test.mobile(mobile)) {
			return uni.showToast({
				title: '请输入正确的手机号',
				icon: "none"
			})
		}
		if (!weixin) {
			return uni.showToast({
				title: '请输入微信',
				icon: "none"
			})
		}
		if (!uni.$u.test.wx(weixin) && !uni.$u.test.mobile(weixin)) {
			return uni.showToast({
				title: '请输入正确的微信号',
				icon: "none"
			})
		}

		if (!category_text) {
			return uni.showToast({
				title: '请选择期望职位',
				icon: "none"
			})
		}
		if (!district_text) {
			return uni.showToast({
				title: '请选择期望地区',
				icon: "none"
			})
		}
		if (!wage_text) {
			return uni.showToast({
				title: '请选择期望薪资',
				icon: "none"
			})
		}

		if (!data.value.jobStatus) {
			return uni.showToast({
				title: '请选择求职状态',
				icon: "none"
			})
		}
		// data.value.dataList.basic.enter_job_time = uni.$u.timeFormat(new Date(data.value.dataList.basic
		// 	.enter_job_time), 'yyyy-mm')
		let params = {
			basic: data.value.dataList.basic,
			contact: data.value.dataList.contact
		}
		let jobStatusId = data.value.jobStatusId
		let formData = data.value.formData

		let res = await uni.$u.api.step1(
			params
		)
		let dres = await uni.$u.api.step2(
			formData
		)
		let wres = await uni.$u.api.step3({
			current: jobStatusId,
			enter_job_time: data.value.dataList.basic.enter_job_time
		})
		if (wres && res && dres) {
			uni.$u.toast('保存成功')
			setTimeout(() => {
				uni.navigateTo({
					//关闭当前页面，跳转到应用内的某个页面。
					url: '/pages/pagesPersonal/Initial/Initial2'
				});
			}, 500)
		}

	}
	// 职位确定事件
	const categoryConfirm = (e) => {
		data.value.formData.category1 = e[0].value
		data.value.formData.category2 = e[1].value
		data.value.formData.category3 = e[2].value
		data.value.formData.category_text = e[2].label
	}
	// 地区确定事件
	const districtConfirm = (e) => {
		data.value.formData.district1 = e[0].value
		data.value.formData.district_text = e[0].label
	}
	// 薪资确定事件
	const wageConfirm = ({
		min,
		max
	}) => {
		data.value.formData.minwage = min
		data.value.formData.maxwage = max
		data.value.formData.wage_text = `${min / 1000}K~${max/1000}K`
		data.value.isWage = false
	}
	// 获取字典表数据
	const getClassifyData = async () => {
		let params = {
			type: 'resumeNature,jobcategory,citycategory,trade'
		}
		let res = await uni.$u.api.getClassifyInfo({
			params
		})
		data.value.districtList = res.citycategory
		data.value.categoryList = res.jobcategory
	}

	// 获取学历列表
	const getEducationList = async () => {
		let params = {
			type: 'education'
		}
		let res = await uni.$u.api.getClassifyInfo({
			params
		})
		if (res) {
			data.value.columns = res
		}

	}
	onLoad(() => {
		// ios侧滑返回功能（关闭）
		var pages = getCurrentPages();
		var page = pages[pages.length - 1];
		// #ifdef APP-PLUS
		var currentWebview = page.$getAppWebview();
		currentWebview.setStyle({
			popGesture: 'none'
		});
		// #endif
	})
	onShow(() => {
		init()
	})
</script>

<style lang="scss">
	page,
	body {
		width: 100vw;
		height: 100vh;
		background-color: #fff;
	}

	.more {

		width: 100%;
		height: 100rpx;
		display: flex;
		justify-content: center;
		align-items: center;

		image {
			margin-left: 20rpx;
			width: 40rpx;
			height: 40rpx;
		}
	}

	:deep .u-flex-1 {
		margin-left: 0 !important;
	}

	.btnBox {
		width: 100vw;
		height: 120rpx;
		display: flex;
		align-items: center;
		justify-content: center;

		button {
			width: 684rpx;
			background-color: #0E6FF7;
			color: #fff;
			height: 68rpx;
			line-height: 68rpx;
			font-size: 28rpx;
			text-align: center;
			font-family: Arial;

			&::after {
				border: none;
			}

		}

	}

	:deep .u-form {
		padding: 30rpx;
	}

	:deep .u-input__input {
		font-size: 30rpx !important;
	}

	:deep .u-field__input-wrap {
		width: 500rpx !important;
	}


	.titleleft {
		width: calc(100vw - 64rpx);
		display: flex;
		justify-content: space-between;
		align-items: center;
		height: 100rpx;
	}

	:deep .u-cell--required {
		&::before {
			top: 24px !important;
		}

	}

	:deep .u-cell {
		height: 200rpx;
	}
</style>